package com.example.stretching;

import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LauncherApps.Callback;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.example.db.DLog;
import com.example.db.DbopenHelper;
import com.example.db.Databases.CreateDB;

/**
 * 
 *두번째 탭
 *Main에서 insert한 DB정보를 표시하고 클릭을 하면 Cursor를 통해서 modifySchedule에 정보를 넘겨줌
 * longclick을 하면 지워짐
 * @author DS
 */
public class RealMainActivity extends Activity implements OnItemClickListener, OnItemLongClickListener{
		Button addSchedule;
		public static ListView mListView;
		WebView webview;
		AlarmAdapter arrayAdapterAlarmList;


		ListView lv_set;
		private Context mContext;
		protected static DbopenHelper mDbOpenHelper;
		private static final String TAG = "TestDataBaseActivity";
//		private static final int position = 0;
		public static final int MON = 0;
		public static final int TUE = 1;
		public static final int WED = 2;
		public static final int THU = 3;
		public static final int FRI = 4;
		public static final int SAT = 5;
		public static final int SUN = 6;
		TextView text0;


		
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.real_main);
		setLayout();
		mContext = getApplicationContext();
		

		mDbOpenHelper = new DbopenHelper(this);
		mDbOpenHelper.open();
		mListView = (ListView) findViewById(R.id.listView_Set);
		mListView.setOnItemLongClickListener(longClickListener);
		mListView.setOnItemClickListener(ClickListener);
		arrayAdapterAlarmList = new AlarmAdapter(mContext,
				MainActivity.arrayListAlarmTimeItem);

		arrayAdapterAlarmList = new AlarmAdapter(mContext,
				MainActivity.arrayListAlarmTimeItem);
		mDbOpenHelper = new DbopenHelper(this);
	       
		MainActivity.arrayListAlarmTimeItem = new ArrayList<AlarmData>();
		AlarmAdapter.arrayAdapterAlarmList = new AlarmAdapter(this, MainActivity.arrayListAlarmTimeItem);
		mListView.setAdapter(AlarmAdapter.arrayAdapterAlarmList);
		
	
		doWhileCursorToArray.doWhileCursorToArray();
		
//		Log.d("id", intent.getStringExtra("data_id"));
//		Log.d("modify", tue);
	
		MainActivity.arrayListAlarmTimeItem.clear();
		doWhileCursorToArray.doWhileCursorToArray();
		AlarmAdapter.arrayAdapterAlarmList.setArrayList(MainActivity.arrayListAlarmTimeItem);
		AlarmAdapter.arrayAdapterAlarmList.notifyDataSetChanged();
		

		MainActivity.mCursor.close();
			addSchedule = (Button)findViewById(R.id.addBtn);
			addSchedule.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					try{
					
					Intent intent0 = new Intent(RealMainActivity.this, MainActivity.class);  
					startActivity(intent0);
					}catch(Exception e){
						return;
					}
					}
			});
		
	}
	OnItemLongClickListener longClickListener = new OnItemLongClickListener() {
		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int position, long arg3) {

			DLog.e(TAG, "position = " + position);
			try{
			boolean result = mDbOpenHelper.deleteColumn(MainActivity.arrayListAlarmTimeItem.get(position).hh,
					MainActivity.arrayListAlarmTimeItem.get(position).hh2);
			// DLog.e(TAG, "result = " + result);
			DLog.e("FROIMTIME", Integer.toString(MainActivity.arrayListAlarmTimeItem.get(position).hh));
			DLog.e("TOTIME", Integer.toString(MainActivity.arrayListAlarmTimeItem.get(position).hh2));

			if (result) {
				MainActivity.arrayListAlarmTimeItem.remove(position);
				AlarmAdapter.arrayAdapterAlarmList.setArrayList(MainActivity.arrayListAlarmTimeItem);
				AlarmAdapter.arrayAdapterAlarmList.notifyDataSetChanged();

			} else {

				Toast.makeText(getApplicationContext(), "INDEX ERROR",
						Toast.LENGTH_LONG).show();
			}
			
	
		}catch(Exception e){
//			Intent intent = new Intent(RealMainActivity.this, RealMainActivity.class); 
//			startActivity(intent); 
			finish();
		}

			return false;
		
		}
	};
	
	  OnItemClickListener ClickListener = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) 
			{		try{
				
				Cursor cursor = mDbOpenHelper.getColumn(MainActivity.arrayListAlarmTimeItem.get(position)._id, 
						(MainActivity.arrayListAlarmTimeItem.get(position).mon), 
						(MainActivity.arrayListAlarmTimeItem.get(position).tue), 
						(MainActivity.arrayListAlarmTimeItem.get(position).wed), 
						(MainActivity.arrayListAlarmTimeItem.get(position).thu), 
						(MainActivity.arrayListAlarmTimeItem.get(position).fri), 
						(MainActivity.arrayListAlarmTimeItem.get(position).sat), 
						(MainActivity.arrayListAlarmTimeItem.get(position).sun), 
						(MainActivity.arrayListAlarmTimeItem.get(position).hh), 
						(MainActivity.arrayListAlarmTimeItem.get(position).mm), 
						(MainActivity.arrayListAlarmTimeItem.get(position).hh2), 
						(MainActivity.arrayListAlarmTimeItem.get(position).mm2),
						(MainActivity.arrayListAlarmTimeItem.get(position).onoff));

				
//				startManagingCursor(cursor);
//				SQLiteDatabase db = mDbOpenHelper.getWritableDatabase();
//				AlarmAdapter.arrayAdapterAlarmList.setArrayList(MainActivity.arrayListAlarmTimeItem);
//				AlarmAdapter.arrayAdapterAlarmList.notifyDataSetChanged();
						
				//리스트를 눌렀을 때 리스트에 해당하는 data 정보를 modifyschedule에 넘겨줌
				Intent intent0 = new Intent(RealMainActivity.this, modifyScheduleActivity.class);
		
				intent0.putExtra("data_id",  cursor.getString(cursor.getColumnIndex("_id")));
				intent0.putExtra("data_mon",  cursor.getString(cursor.getColumnIndex("mon")));
				intent0.putExtra("data_tue",  cursor.getString(cursor.getColumnIndex("tue")));
				intent0.putExtra("data_wed",  cursor.getString(cursor.getColumnIndex("wed")));
				intent0.putExtra("data_thu",  cursor.getString(cursor.getColumnIndex("thu")));
				intent0.putExtra("data_fri",  cursor.getString(cursor.getColumnIndex("fri")));
				intent0.putExtra("data_sat",  cursor.getString(cursor.getColumnIndex("sat")));
				intent0.putExtra("data_sun",  cursor.getString(cursor.getColumnIndex("sun")));
				intent0.putExtra("data_hour1",  cursor.getInt(cursor.getColumnIndex("fromtimehour")));
				intent0.putExtra("data_hour2",  cursor.getInt(cursor.getColumnIndex("fromtimeminute")));
				intent0.putExtra("data_hour3",  cursor.getInt(cursor.getColumnIndex("totimehour")));
				intent0.putExtra("data_hour4",  cursor.getInt(cursor.getColumnIndex("totimeminute")));
				intent0.putExtra("onoff", cursor.getString(cursor.getColumnIndex("onoff")));
			
				startActivity(intent0);
				 try { Thread.sleep(500);
	                } catch (InterruptedException e) {
	                    e.printStackTrace();
	                }
				finish();
				}catch(Exception e){
					Toast.makeText(getApplicationContext(), "For getting data, return to Mainpage",
							Toast.LENGTH_LONG).show();
					finish();
					
					Intent intent9 = new Intent(RealMainActivity.this, tabMainActivity.class);
					startActivity(intent9);

				}
			}
		  };

		  
		
			private void setLayout() {
		        tabMainActivity.tabHost = (TabHost) findViewById(android.R.id.tabhost);
				mListView = (ListView) findViewById(R.id.listView_Set);
			}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			// TODO Auto-generated method stub
			return false;
		}
		
 
}
